<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/12/31 0031
 * Time: 15:59
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);


namespace app\common\business;

use app\common\model\MallMemberAddress as ModelObj;
use app\common\validate\Address as ValObj;
use think\Exception;
use think\facade\Log;

class Address extends Base
{
    /**
     * @var ValObj
     */
    protected ValObj $valObj;
    /**
     * @var ModelObj
     */
    protected ModelObj $modelObj;

    /**
     *
     */
    public function __construct()
    {
        $this->valObj = new ValObj();
        $this->modelObj = new ModelObj();
    }

    /**
     * @param array $data
     * @param string $scene
     * @throws Exception
     */
    protected function isValidateAdd(array $data = [], string $scene = 'add')
    {
        if (!$this->valObj->check($data)) {
            throw new Exception($this->valObj->getError());
        }
    }

    /**
     * 指定收货地址
     * @param array $where
     * @param string $field
     * @return array|\think\Model|null
     * @throws Exception
     */
    public function getaddressInfo(array $where,string $field='*'){
        try{
            $info =$this->modelObj->getModelByInfo($where,$field);
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        if(!$info){
            $info=[];
        }
        return $info;
    }

    public function saveData(array $where,array $data){
        try{
            $list =$this->modelObj->where($where)->save($data);
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
//        if($list){
//            foreach ($list as $k=>$v){
//                $data['id'] =$v['id'];
//                $list->save($data);
//            }
//        }
        return true;
    }

    /**
     * 编辑收货地址
     * @param $data
     * @return bool
     * @throws Exception
     */
    public function updateByData($data){
        //验证字段
        $this->isValidateAdd($data);
        try{
            $info =$this->modelObj->getModelByInfo([['id','=',$data['id']]],'*');
            $this->modelObj->where([['openid','=',$data['openid']]])->save(array('isdefault'=>0));
        }catch (\Exception $e){
            throw new Exception($e->getMessage());
        }
        if($info){
            $info->save($data);
        }else{
            $this->modelObj->save($data);
        }
        return true;
    }

    /**
     * 删除收货地址
     * @param $where
     * @return bool
     * @throws Exception
     */
    public function deleteByAddress($where){
        try{
            $list =$this->modelObj->deleteByIds($where);
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        return true;
    }

    /**
     * 我的收货地址
     * @param array $where
     * @param string $field
     * @return array
     * @throws Exception
     */
    public function getaddresslist(array $where,string $field='*'){
        try{
            $list =$this->modelObj->getModelByListToArray($where,$field,'isdefault desc,create_time desc');
        }catch (\Exception $e){
            throw new Exception("查询失败");
        }
        return $list;
    }
}